package cn.thornbird.orgsync.controller.export;

import cn.thornbird.orgsync.annotation.CurrentUser;;
import cn.thornbird.orgsync.entity.VerificationCode;
import cn.thornbird.orgsync.model.Column;
import cn.thornbird.orgsync.model.PageList;
import cn.thornbird.orgsync.model.UserData;
import cn.thornbird.orgsync.service.VerificationCodeService;
import cn.thornbird.orgsync.util.ExcelUtil;
import cn.thornbird.orgsync.util.JsonUtil;
import cn.thornbird.orgsync.util.WebUtil;
import cn.thornbird.orgsync.vo.request.VerificationCodeQueryRequest;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/export")
@Api
@AllArgsConstructor
public class ExportController {

    public static final int MAX_ROWS = PageList.MAX_PAGE_SIZE;

    private final VerificationCodeService verificationCodeService;

    @PostMapping("verificationCodes")
    public void exportVerificationCodes(@CurrentUser @ApiIgnore UserData userData,
                                        @RequestBody VerificationCodeQueryRequest verificationCodeQueryRequest,
                                        HttpServletResponse response) throws IOException {
        if (!checkAuthority(userData, response)) {
            return;
        }

        List<VerificationCode> verificationCodes = verificationCodeService.query(verificationCodeQueryRequest,
                0, MAX_ROWS).getList();
        List<Column> columns = new ArrayList<>();
        columns.add(new Column("mobile", "手机", 13));
        columns.add(new Column("code", "验证码", 9));
        columns.add(new Column("usage", "用途", 10, HorizontalAlignment.CENTER));
        columns.add(new Column("success", "发送成功", 9, HorizontalAlignment.CENTER));
        columns.add(new Column("used", "已使用", 9, HorizontalAlignment.CENTER));
        columns.add(new Column("createdAt", "创建时间", 22));
        columns.add(new Column("responseBody", "接口返回数据", 32));
        columns.add(new Column("errorMessage", "错误消息", 32));
        List<Map<String, Object>> records = new ArrayList<>();
        for (VerificationCode verificationCode : verificationCodes) {
            Map<String, Object> record = JsonUtil.toMap(verificationCode);
            String usage = "";
            switch (verificationCode.getUsage()) {
                case SignUp:
                    usage = "注册用户";
                    break;
                case ResetPassword:
                    usage = "重置密码";
                    break;
                default:
                    break;
            }
            record.put("usage", usage);
            records.add(record);
        }
        SXSSFWorkbook workbook = ExcelUtil.createWorkbook();
        ExcelUtil.createSheet(workbook, "验证码", columns, records);
        ExcelUtil.export(response, workbook, "验证码");
    }

    private boolean checkAuthority(UserData userData, HttpServletResponse response) throws IOException {
        if (userData.hasAuthority(UserData.ADMIN)) {
            return true;
        }
        String errorMessage = String.format("禁止访问，没有%s权限", UserData.ADMIN);
        WebUtil.writeExcelErrorMessageToResponse(response, errorMessage);
        return false;
    }

}
